< previous page page_97 next page >

Page 97
0097-01.jpg
Figure 5.1
Graphical user interfaces go hand in hand with event-driven programming.
Another advantage of event-driven programming is that you can use the events to provide immediate feedback to the user. For example, you can program an event to verify a user's entry as soon as the user finishes typing. Then, if a problem occurs with what the user entered, he or she knows immediately and can fix it. This type of feedback is handled by code, such as that shown in Listing 5.1, which ensures that the user has entered required information in the text box.
Listing 5.1 EVENTS.FRMCode to Verify Entry of a Last Name
Private Sub txtLastName_LostFocus()
    If Trim(txtLastName.Text) = "" Then
        smsg = "You must enter your Last Name."
        MsgBox smsg, vbExclamation, "Warning"
        txtLastName.SetFocus
    End If

End Sub
Handling Events in Your Programs
In a typical program, many events can occur and many user actions can trigger these events. Program events can be triggered by actions such as the user clicking a mouse button, pressing a key, moving the mouse, changing the value of the information in a control, or switching to another window in the program. The program itself can cause events to occur. Windows events can also be triggered by system functions such as a timer, or even by external factors such as receiving an e-mail message. Hundredsif not thousandsof events can occur in any given program. Your program should be able to isolate the events that are relative to the program's function. Visual Basic makes this task easy.
Determining When an Event Has Occurred
When Windows detects a user action destined for your program, it sends your program a message. The Visual Basic runtime kernel embedded in your program interprets the message and, if appropriate, raises a corresponding event. If you've written code to handle this event, Visual Basic invokes your code.

 
< previous page page_97 next page >